package com.hnkywl.sts.dao.organ;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.organ.Lj;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-4
 * Time: 上午12:04
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class LjwhKclsDao extends SimpleHibernateTemplate<Lj, Long> {
    public Page<Lj> findLjKclsList(String jyjg, Long jyjgId,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Lj> page = new Page<Lj>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (jyjgId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jyjg.id = ? ");
            param.add(jyjgId);
        }
        if (jyjg != null && !"".equals(jyjg)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jyjg.jc like ? ");
            param.add("%" + jyjg.trim() + "%");
        }
        //==============记录数量===============
        hql = " select count(*) from Lj where lx=4" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Lj where lx=4" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public List<Lj> findLjwhKcls(Long jyjgId) {
        return find("from Lj where lx=4 and jyjg.id = ? and sfdj = false order by dateCreated desc",jyjgId);
    }


    public Boolean checkUnique(Long id) {
        String  hql = "from Lj where lx = 4 and jyjg.id = ? ";
        return this.existsQuery(hql,id);
    }
}
